package cn.lili.modules.auth.serviceimpl;

import cn.lili.cache.Cache;
import cn.lili.cache.CachePrefix;
import cn.lili.common.exception.ServiceException;
import cn.lili.common.security.AuthUser;
import cn.lili.common.security.context.UserContext;
import cn.lili.common.security.enums.SceneEnums;
import cn.lili.modules.auth.service.AuthService;
import cn.lili.modules.member.client.UserClient;
import cn.lili.modules.member.entity.vo.UserVO;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

/**
 * AuthServiceImpl
 *
 * @author Chopper
 * @version v1.0
 * 2022-08-31 15:22
 */
@Service
@RequiredArgsConstructor
public class AuthServiceImpl implements AuthService {

    private final Cache cache;

    private final UserClient userClient;

    @Override
    public void logout(SceneEnums sceneEnums) {
        AuthUser currentUser = UserContext.getCurrentUser();
        if (currentUser != null) {
            cache.remove(CachePrefix.ACCESS_TOKEN.getPrefix(sceneEnums, currentUser.getId()));
            cache.remove(CachePrefix.REFRESH_TOKEN.getPrefix(sceneEnums, currentUser.getId()));
        }
    }

    @Override
    public UserVO info() {
        UserVO userVO;
        try {
            userVO = userClient.info();
        } catch (Exception e) {
            e.printStackTrace();
            throw new ServiceException(e.getMessage());
        }
        return userVO;
    }
}
